/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import entities.ViewScheduler;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author viev
 */
public class ViewDoctorSchedulerTableModel extends AbstractTableModel {
    ArrayList<ViewScheduler> list;
    ArrayList<ViewScheduler> disp;

    public ViewDoctorSchedulerTableModel() {
        list = new ArrayList<ViewScheduler>();
        disp = new ArrayList<ViewScheduler>();
    }

    public ViewDoctorSchedulerTableModel(ArrayList<ViewScheduler> list) {
        this.list = new ArrayList<ViewScheduler>(list);
        this.disp = new ArrayList<ViewScheduler>(list);
    }

    @Override
    public int getRowCount() {
        return disp.size();
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ViewScheduler record = disp.get(rowIndex);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd YYYY");        
        Date startDate = record.getStartDate();
        Date endDate = record.getEndDate();
        switch (columnIndex) {
            case 0:
                return record.getLoginID();
            case 1:
                return record.getName();
            case 2:
                return record.getDepartmentName();
            case 3:
                return record.getShiftName();
            case 4:
                return dateFormat.format(record.getStartDate());
            case 5:
                if(endDate != null)
                    return dateFormat.format(record.getEndDate());
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            case 2:
                return "Department";
            case 3:
                return "Shift";
            case 4:
                return "StartDate";
            case 5:
                return "EndDate";
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    public void addViewScheduler(ViewScheduler record) {
        list.add(record);
        disp.add(record);
        fireTableDataChanged();
    }

    public ViewScheduler getViewSchedulerAt(int index) {
        return disp.get(index);
    }

    public void updateViewScheduler(ViewScheduler record) {
        list.set(list.indexOf(record), record);
        int index = disp.indexOf(record);
        if (index != -1) {
            disp.set(index, record);
        }
        fireTableDataChanged();
    }

    public void deleteViewScheduler(ViewScheduler record) {
        list.remove(record);
        disp.remove(record);
        fireTableDataChanged();
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }

    public boolean containtPK(int eid, int did, int sid, Date date) {
        for (ViewScheduler record : list) {
            if (record.getEmployeeID() == eid && record.getDepartmentID() == did
                    && record.getShiftID() == sid && record.getStartDate().getTime() == date.getTime()) {
                return true;
            }
        }
        return false;
    }

    public void filter(String id, String name, int depID, int level) {
        disp.clear();
        for (ViewScheduler record : list) {
            boolean accept = true;
            if (id != null && id.length() != 0) {
                int length = id.length();
                if (record.getLoginID().length() >= length) {
                    if (!record.getLoginID().substring(0, length).equalsIgnoreCase(id)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (name != null && name.length() != 0) {
                int length = name.length();
                if (record.getName().length() >= length) {
                    if (!record.getName().substring(0, length).equalsIgnoreCase(name)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (depID != 0) {
                if (record.getDepartmentID() != depID) {
                    accept = false;
                }
            }
            if (level != 0) {
                if (record.getLevel() != level) {
                    accept = false;
                }
            }
            if (accept) {
                disp.add(record);
            }
        }
        fireTableDataChanged();
    }
}
